Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(maitake): handle spurious WaitCell polls #453

Merged
merged 6 commits into from
Jul 23, 2023
Merged

Conversation

hawkw
Copy link
Owner

@hawkw hawkw commented Jul 22, 2023

This branch changes the Wait future for maitake::sync::WaitCell to
handle spurious polls correctly. Currently, a wait_cell::Wait future
assumes that if it's ever polled a second time, that means its waker was
woken. However, there might be other reasons that a stack of futures
containing a Wait is polled again, and the Wait future will
incorrectly complete immediately in that case.

This branch fixes this by replacing the bool field in Wait that's
set on first poll with an "event count" stored in the remaining
WaitCell state bits. Now, when a Wait is created, it loads the
current event count, and calls to wake() and close() increment the
event count. The Wait future then checks if the event count has gone
up when it's polled, rather than just checking if it's ever been polled
before. This allows the Wait future to determine if it is being polled
because the WaitCell woke it up, or if it's being polled because some
other future decided to poll it. This also has the side benefit of
fixing racy scenarios where the WaitCell is woken between when the
Wait future is created and when it's polled for the first time.

Fixes #449

This branch changes the `Wait` future for `maitake::sync::WaitCell` to
handle spurious polls correctly. Currently, a `wait_cell::Wait` future
assumes that if it's ever polled a second time, that means its waker was
woken. However, there might be other reasons that a stack of futures
containing a `Wait` is polled again, and the `Wait` future will
incorrectly complete immediately in that case.

This branch fixes this by replacing the `bool` field in `Wait` that's
set on first poll with an "event count" stored in the remaining
`WaitCell` state bits. Now, when a `Wait` is created, it loads the
current event count, and calls to `wake()` and `close()` increment the
event count. The `Wait` future then checks if the event count has gone
up when it's polled, rather than just checking if it's ever been polled
before. This allows the `Wait` future to determine if it is being polled
because the `WaitCell` woke it up, or if it's being polled because some
other future decided to poll it. This *also* has the side benefit of
fixing racy scenarios where the `WaitCell` is woken between when the
`Wait` future is created and when it's polled for the first time.

Fixes #449
@hawkw hawkw requested a review from jamesmunns July 22, 2023 18:50
@hawkw
Copy link
Owner Author

hawkw commented Jul 22, 2023

@jamesmunns it would be nice to confirm that this actually fixes the weird behavior you were seeing in mnemOS...

@hawkw hawkw enabled auto-merge (squash) July 23, 2023 16:25
@hawkw hawkw merged commit 46b1131 into main Jul 23, 2023
17 checks passed
@hawkw hawkw deleted the eliza/waitcell-double-poll branch July 23, 2023 16:30
hawkw added a commit that referenced this pull request Jul 24, 2023
This branch rewrites the `maitake::sync::WaitCell` type a bit to fix
potential lost wakeups accidentally introduced in #453. In addition, it
changes the API a bit:
- Replace `register_wait` with `poll_wait`, which I think is a bit
  nicer,
- Removed the `RegisterError::Waking` variant since `poll_wait` returns
  `Ready` if the cell was woken, and renamed `RegisterError` to just
  `Error`,
- Introduced a `WaitCell::subscribe` method to eagerly pre-register the
  task's `Waker` in the `WaitCell`. This is intended to be used to
  register the task to be woken *before* performing an operation that
  results in a wakeup, such as enabling an interrupt that will wake the
  task.

Closes #455
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

WaitCell::wait does not tolerate spurious polls
1 participant